﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using System.Data;
using Sinacor.Infra.Service.Data;
using Sinacor.Infra.Service.Security.Authorization;
using Sinacor.Infra.Service.Common;

namespace Sinacor.Servico.Bovespa.Corretagem.ResourceAccess
{
    //Task  : 5617 
    // Data : 11/03/2009
    // Autor:Apsilva
    /// <summary>
    /// Classe de Acesso a Base
    /// </summary>
    public class DescontoCliente : BaseResourceAccess
    {
        #region Construtor

        /// <summary>
        /// Construtor padrão
        /// </summary>
        public DescontoCliente()
            : base(AuthorizationContext.Current.CompanyId)
        {
        }

        #endregion

        #region AlterarDescontoCliente
        /// <summary>
        /// Método AlterarDescontoCliente
        /// </summary>
        public void AlterarDescontoCliente(BusinessEntity.DescontoCliente entity)
        {
            StringBuilder sql = new StringBuilder();

            sql.Append(@"UPDATE TORCORCLI  
							                SET   PC_REDACR = :PercentualDescontoAcresimo
							                WHERE CD_CLIENTE = :CodigoCliente
							                      AND NM_TIPO = :TipoOperacao
                      ");

            using (DbCommand command = this.DataBase.GetSqlStringCommand(sql.ToString()))
            {
                this.DataBase.AddInParameter(command, "PercentualDescontoAcresimo", DbType.Decimal, entity.PercentualDescontoAcresimo);
                this.DataBase.AddInParameter(command, "CodigoCliente", DbType.Int32, entity.CodigoCliente);
                this.DataBase.AddInParameter(command, "TipoOperacao", DbType.AnsiString, EnumConverter.ToString<BusinessEntity.Enum.TipoOperacaoDesconto?>(entity.TipoOperacao));
                this.DataBase.ExecuteNonQuery(command);

            }
        }

        #endregion


        #region ConsultarDescontosClientes
        /// <summary>
        /// Método ConsultarDescontosClientes
        /// </summary>
        public List<BusinessEntity.DescontoCliente> ConsultarDescontosClientes(BusinessEntity.Parameter.FiltroDescontoCliente request)
        {
            List<BusinessEntity.DescontoCliente> response = null;
            StringBuilder sql = new StringBuilder();

            sql.Append(@"SELECT B.CD_CLIENTE CodigoCliente,
							    B.NM_APELIDO ApelidoCliente,
                                A.NM_TIPO TipoOperacao,
                                A.PC_REDACR PercentualDescontoAcresimo
                         FROM   TORCORCLI A INNER JOIN TSCCLIBOL B
							    ON A.CD_CLIENTE = B.CD_CLIENTE 
                                WHERE 
                            ");
            sql.AppendLine(new Sinacor.Servico.Bovespa.Comum.ResourceAccess.Acesso().ConsultarListaAssessores("B.CD_ASSESSOR", 0, 9999999));
            sql.Append(" AND");

            if (request.CodigoCliente != null)
            {
                sql.Append(" B.CD_CLIENTE = :CodigoCliente"); 
                sql.Append(" AND");
            }
            if (!String.IsNullOrEmpty(request.ApelidoCliente))
            {
                sql.Append("  B.NM_APELIDO = :ApelidoCliente ");
                sql.Append(" AND");
            }

            if (request.TipoOperacao != null)
            {
                sql.Append("   A.NM_TIPO = :TipoOperacao ");
                sql.Append(" AND");
            }


            if (request.PercentualDescontoAcresimo != null)
            {
                sql.Append(" A.PC_REDACR = :PercentualDescontoAcresimo ");
                sql.Append(" AND");
            }
            sql.Remove(sql.Length - 3, 3); 
            sql.Append( " ORDER BY B.NM_APELIDO" ); 

            using (DbCommand command = this.DataBase.GetSqlStringCommand(sql.ToString()))
            {
                if (request.CodigoCliente != null) 
                    this.DataBase.AddInParameter(command, "CodigoCliente", DbType.Int32, request.CodigoCliente);
                
                if(!string.IsNullOrEmpty( request.ApelidoCliente))
                    this.DataBase.AddInParameter(command, "ApelidoCliente", DbType.AnsiString, request.ApelidoCliente);

                if (request.TipoOperacao != null)
                    this.DataBase.AddInParameter(command, "TipoOperacao", DbType.AnsiString, EnumConverter.ToString<BusinessEntity.Enum.TipoOperacaoDesconto?>(request.TipoOperacao));

                if (request.PercentualDescontoAcresimo !=null) 
                    this.DataBase.AddInParameter(command, "PercentualDescontoAcresimo", DbType.Decimal, request.PercentualDescontoAcresimo);
                
                response = this.FindAll<BusinessEntity.DescontoCliente>(command, new ConstructEntity<BusinessEntity.DescontoCliente>(ConstrutorDescontoCliente));
            }

            return response;
        }

        #endregion


        #region ConstrutorConstrutorDescontoCliente
        protected BusinessEntity.DescontoCliente ConstrutorDescontoCliente(IDataReader reader)
        {
            BusinessEntity.DescontoCliente entity = new BusinessEntity.DescontoCliente();

            entity.ApelidoCliente = reader.GetValue<string>("ApelidoCliente");
            entity.CodigoCliente = reader.GetValue<int?>("CodigoCliente");
            entity.PercentualDescontoAcresimo = reader.GetValue<decimal?>("PercentualDescontoAcresimo");
            entity.TipoOperacao = EnumConverter.Parse<Sinacor.Servico.Bovespa.Corretagem.BusinessEntity.Enum.TipoOperacaoDesconto>(reader.GetValue<string>("TipoOperacao"));
            return entity;
        }
        #endregion


        #region ExcluirDescontoCliente
        /// <summary>
        /// Método ExcluirDescontoCliente
        /// </summary>
        public void ExcluirDescontoCliente(BusinessEntity.Parameter.FiltroDescontoCliente request)
        {
            StringBuilder sql = new StringBuilder();

            sql.Append(@"DELETE FROM TORCORCLI  
							WHERE CD_CLIENTE = :CodigoCliente
                                  AND NM_TIPO = :TipoOperacao  
                      ");

            using (DbCommand command = this.DataBase.GetSqlStringCommand(sql.ToString()))
            {
                this.DataBase.AddInParameter(command, "CodigoCliente", DbType.Int32, request.CodigoCliente);
                this.DataBase.AddInParameter(command, "TipoOperacao", DbType.AnsiString, EnumConverter.ToString<BusinessEntity.Enum.TipoOperacaoDesconto?>(request.TipoOperacao));
                this.DataBase.ExecuteNonQuery(command);

            }
        }

        #endregion


        #region IncluirDesconto
        /// <summary>
        /// Método IncluirDescontoCliente
        /// </summary>
        public void IncluirDescontoCliente(BusinessEntity.DescontoCliente request)
        {
            StringBuilder sql = new StringBuilder();

            sql.Append(@"INSERT INTO TORCORCLI (CD_CLIENTE, NM_TIPO, PC_REDACR)   
							                    VALUES (:CodigoCliente, :TipoOperacao, :PercentualDescontoAcresimo)
                      ");

            using (DbCommand command = this.DataBase.GetSqlStringCommand(sql.ToString()))
            {
                this.DataBase.AddInParameter(command, "CodigoCliente", DbType.Int32, request.CodigoCliente);
                this.DataBase.AddInParameter(command, "TipoOperacao", DbType.AnsiString, EnumConverter.ToString<BusinessEntity.Enum.TipoOperacaoDesconto?>(request.TipoOperacao));
                this.DataBase.AddInParameter(command, "PercentualDescontoAcresimo", DbType.Decimal, request.PercentualDescontoAcresimo);
                this.DataBase.ExecuteNonQuery(command);

            }
        }



        #endregion



    }
}
